package com.augurit.common.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.OmGroup;
import com.augurit.common.system.convert.OmGroupConverter;
import com.augurit.common.system.web.org.form.OmGroupForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.lang.CollectionUtils;

/**
 * 工作组DAO类
 */
@Repository
public class OmGroupDao extends BaseDao<OmGroup, Long> {

	/**
	 * 根据工作组ID获取工作组名称
	 * @param id 工作组主键
	 * @return 工作组名称
	 */
	public String getGroupName(Long id) {
		if(id != null){
			OmGroup entity = this.get(id);
			if(entity != null)
				return entity.getGroupName();
		}
		return null;
	}
	
	/**
	 * 保存工作组Form对象
	 * @param form 工作组Form对象
	 */
	public void save(OmGroupForm form){
		
		if(form != null){
			OmGroup entity = null;
			
			//准备VO对象
			if(form != null && form.getGroupId() != null){
				entity = this.get(form.getGroupId());
			}else{
				entity = new OmGroup();
			}
			
			//属性值转换
			OmGroupConverter.convertToEntity(form, entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setGroupId(entity.getGroupId());
		}
	}

	/**
	 * 根据机构ID获取多个业务字典项对象集合
	 * @param orgId 业务字典项机构id
	 * @return 业务字典项对象集合,如果机构id为null,则返回null
	 */
	public List<Long> getGroupCascade(Long orgId){
		List<Long> list = null;
		if(orgId != null){
			list = new ArrayList();
			String hql = "select ps.groupId from OmGroup ps where ps.orgId=?";
			List values = new ArrayList();
			values.add(orgId);
			list = this.find(hql,values);
		}
		return list;
	}
	
	/**
	 * 根据主键ID获取所有子工作组的OmGroupForm列表(不包括它自己)
	 * 方法是通过模糊匹配顺序编号sortNo
	 * @param id 业务字典项主键ID
	 * @return 子工作组OmGroupForm列表
	 */
	public List<OmGroupForm> getAllChildren(Long groupId){
		List<OmGroupForm> list = null;
		if(groupId != null){
			OmGroup entity = this.get(groupId);
			String hql ="from OmGroup ps where ps.groupId <> ? and ps.groupSeq like ?";
			List values = new ArrayList();
			values.add(groupId);
			values.add("%"+entity.getGroupSeq()+"%");
			List<OmGroup> entities = this.find(hql,values);
			
			if(entities != null && entities.size()>0)
				list = OmGroupConverter.convertToForms(entities);
		}
	    return list;
	}
	
	/**
	 * 获取指定工作组的孩子工作组（包括直属和间接的）
	 * @param groupId
	 * @return
	 */
	public List<Long> getAllChildGroupIds(Long groupId){
		if(groupId != null){
			OmGroup entity = this.get(groupId);
			String hql ="select ps.groupId from OmGroup ps where ps.groupId <> ? and ps.groupSeq like ?";
			List values = new ArrayList();
			values.add(groupId);
			values.add("%"+entity.getGroupSeq()+"%");
			return this.find(hql,values);
		}
	    return null;
	}
	
	/**
	 * 当指定工作组删除了子对象后要对该机构的相关计算字段进行更新
	 * @param groupId 工作组主键ID
	 */
	public void updateGroupAfterRemoveChildren(Long groupId){
		if(groupId != null){
			OmGroup entity = this.get(groupId);
			
			int subCount = entity.getSubCount() == null?0:entity.getSubCount()- 1;	//子节点数减1
						
			entity.setSubCount(subCount);
			if(subCount == 0)
				entity.setIsLeaf(OrgConstant.ONE);		//设置为叶子节点
			else
				entity.setIsLeaf(OrgConstant.ZERO);		//设置为非叶子节点
							
			this.save(entity);
		}
	}
	
	/**
	 * 根据工作组id获取直属子工作组OmGroupForm列表
	 * @param groupId 工作组id
	 * @return 子工作组OmGroupForm列表
	 * */
	public List<OmGroupForm> getDirectlyChildren(Long groupId){
		List<OmGroupForm> list = null;
		if(groupId != null){
			String hql = "from OmGroup ps where ps.parentGroupId=?";
			List<Long> values =  new ArrayList<Long>();
			values.add(groupId);
			List<OmGroup> entities = this.find(hql,values);
			
			if(entities != null && entities.size()>0)
				list = OmGroupConverter.convertToForms(entities);
		}
		return list;
	}
	
	/**
	 * 获取顶级工作组form列表
	 * @return 顶级工作组form列表
	 * */
	public List<OmGroupForm> getTopGroups(){
		List<OmGroupForm> list = null;
		String hql = "from OmGroup ps where ps.parentGroupId is null";
		List<OmGroup> entities = this.find(hql);
		if(entities != null && entities.size()>0)
			list = OmGroupConverter.convertToForms(entities);
		return list;
	}
	
	/**
	 * 给指定工作组添加子结点时更新指定工作组相关字段的值
	 * @param entity
	 */
	public void updateGroupAfterAddChildren(OmGroup group){
		if( group != null){
			int subCount = group.getSubCount() == null ? 1 : group.getSubCount() + 1;	//子节点数加1
			group.setSubCount(subCount);
			group.setIsLeaf(OrgConstant.ZERO);
			this.save(group);
		}
	}

	/**
	 * 获取所有的工作组id数组
	 * @return
	 */
	public Long[] getAllGroupIds(){
		String hql = "select ps.groupId from OmGroup ps"; 
		List<Long> list = this.find(hql);
		return CollectionUtils.toLongArray(list);
	}
	
	/**
	 * 获取具有工作组的机构id数组
	 * @return
	 */
	public Long[] getOrgIdsOwnedGroup(){
		String hql = "select distinct ps.orgId from OmGroup ps";
		List<Long> list = this.find(hql);
		
		return CollectionUtils.toLongArray(list);
	}
	
	
	/**
	 * 获取指定机构下的直属工作组的id数组
	 * @param orgId
	 * @return
	 */
	public Long[] getDirectGroupIdsByOrg(Long orgId){
		if(orgId != null){
			String hql = "select ps.groupId from OmGroup ps where ps.orgId = ?";
			List<Long> values = new ArrayList();
			values.add(orgId);
			List list = this.find(hql, values);
			return CollectionUtils.toLongArray(list);
		}
		return null;
	}
	
	
	/**
	 * 获取指定机构下的直属工作组
	 * @param orgId 机构id
	 * @return 工作组form
	 */
	public List<OmGroupForm> getDirectGroupsByOrg(Long orgId){
		if(orgId != null){
			String hql = "from OmGroup ps where ps.orgId = ? and ps.parentGroupId is null";
			List values = new ArrayList();
			values.add(orgId);
			List<OmGroup> entities = this.find(hql, values);
			return OmGroupConverter.convertToFormList(entities);
		}
		return null;
	}
	
	/**
	 * 根据主键ID获取子工作组对象集
	 * @param id 业务字典项主键ID
	 * @return 子工作组对象集
	 */
	public List<OmGroupForm> getChildGroups(Long groupId){
		if(groupId != null){
			String hql ="from OmGroup ps where ps.parentGroupId= ?";
			List<Long> values = new ArrayList<Long>();
			values.add(groupId);
			List<OmGroup> entities= this.find(hql,values); 
			return OmGroupConverter.convertToForms(entities);
		}
		return null;
	}
	
	/**
	 * 获取工作组下的直属工作组
	 * @param groupId 工作组id
	 * @return
	 */
	public List<OmGroupForm> getDirectGroupsByGroup(Long groupId){
		if(groupId != null){
			String hql = "from OmGroup ps where ps.parentGroupId = ?";
			List values = new ArrayList();
			values.add(groupId);
			List list = this.find(hql,values);
			return OmGroupConverter.convertToFormList(list);
		}
		return null;
	}
	
	/**
	 * 获取一组工作组的直属父机构
	 * @param groupIds
	 * @return
	 */
	public Long[] getDirectParentOrgIds(Long...groupIds){
		if(groupIds != null && groupIds.length > 0){
			String hql = "select distinct ps.orgId from OmGroup ps where " + HqlUtils.buildOrCondition("ps", "groupId", groupIds);
			List list = this.find(hql);
			return CollectionUtils.toLongArray(list);
		}
		return null;
	}
	
	/**
	 * 获取所有的工作组
	 * @return 获取所有的工作组列表
	 */
	public List<OmGroupForm> getAllGroups(){
		List<OmGroup> entities = this.getAll();
		return OmGroupConverter.convertToFormList(entities);
	}
	
	
}
